﻿Public Class Mapper
    Public Shared Function ToJobHistory(data As JobHistory, Optional includeResults As Boolean = False) As DataContract.JobHistory
        Dim jh As New DataContract.JobHistory
        With jh
            .JobId = data.JobId
            .JobName = data.Job.Name
            .CronGuid = data.CronGuid
            .StartDTO = data.StartDTO
            .EndDTO = data.EndDTO
            .Status = data.Status
            .StepHistory = data.Job.Steps.Select(Function(x) x.Script.History.Where(Function(y) y.CronGuid = data.CronGuid).Select(Function(y) ToScriptHistory(y, includeResults))).ToList
        End With
        Return jh
    End Function

    Public Shared Function ToScriptHistory(data As ScriptHistory, Optional includeResults As Boolean = False) As DataContract.ScriptHistory
        Dim sh As New DataContract.ScriptHistory
        With sh
            .ScriptId = data.ScriptId
            .ScriptName = data.Script.Name
            .CronGuid = data.CronGuid
            .StartDTO = data.StartDTO
            .EndDTO = data.EndDTO
            .StartDTO = data.Status
            .Parameters = data.Parameters
            If includeResults Then
                .Results = data.Results.Select(Function(x) ToScriptResult(x)).ToList
            End If
        End With

        Return sh
    End Function

    Public Shared Function ToScriptResult(data As ScriptResult) As DataContract.ScriptResult
        Return New DataContract.ScriptResult With {.Generated = data.Generated, .Type = data.Type, .Message = data.Message}
    End Function

    Public Shared Function ToScript(data As Script) As DataContract.Script
        Return New DataContract.Script With {.ScriptId = data.ScriptId,
                                             .Name = data.Name,
                                             .Description = data.Description,
                                             .Code = data.Code,
                                             .SingleInstance = data.SingleInstance,
                                             .CategoryId = data.CategoryId,
                                             .CategoryName = data.Category.Name,
                                             .CategoryDescription = data.Category.Description,
                                             .Tags = data.Tags.Select(Function(x) x.Name).ToArray
                                            }
    End Function

    Public Shared Function FromScript(data As DataContract.Script) As Script
        Return New Script With {.ScriptId = data.ScriptId,
                                .Name = data.Name,
                                .Description = data.Description,
                                .Code = data.Code,
                                .SingleInstance = data.SingleInstance}
    End Function

    Public Shared Function ToJob(data As Job) As DataContract.Job
        Dim j As New DataContract.Job
        With j
            .JobId = data.JobId
            .Name = data.Name
            .ForceDebug = data.ForceDebug
            .ForceVerbose = data.ForceVerbose
            .ForceStopOnError = data.ForceStopOnError
            .SingleInstance = data.SingleInstance
            .NextRun = data.NextRun
            .Interval = data.Interval
            .IntervalType = data.IntervalType
            .IntervalScript = data.IntervalScript
            .Steps = data.Steps.Select(Function(x) New DataContract.JobStep With {.JobId = x.JobId,
                                                                                    .StepNumber = x.StepNumber,
                                                                                    .ScriptId = x.ScriptId,
                                                                                    .ForceDebug = x.ForceDebug,
                                                                                    .ForceVerbose = x.ForceVerbose,
                                                                                    .ForceStopOnError = x.ForceStopOnError,
                                                                                    .ContinueOnFailure = x.ContinueOnFailure,
                                                                                    .Parameters = x.Parameters}).ToList
        End With
        Return j
    End Function

    Public Shared Function FromJob(data As DataContract.Job) As Job
        Dim j As New Job
        With j
            .JobId = data.JobId
            .Name = data.Name
            .ForceDebug = data.ForceDebug
            .ForceVerbose = data.ForceVerbose
            .ForceStopOnError = data.ForceStopOnError
            .SingleInstance = data.SingleInstance
            .NextRun = data.NextRun
            .Interval = data.Interval
            .IntervalType = data.IntervalType
            .IntervalScript = data.IntervalScript
            If data.Steps IsNot Nothing Then
                .Steps = data.Steps.Select(Function(x) New JobStep With {.JobId = x.JobId,
                                                                            .StepNumber = x.StepNumber,
                                                                            .ScriptId = x.ScriptId,
                                                                            .ForceDebug = x.ForceDebug,
                                                                            .ForceVerbose = x.ForceVerbose,
                                                                            .ForceStopOnError = x.ForceStopOnError,
                                                                            .ContinueOnFailure = x.ContinueOnFailure,
                                                                            .Parameters = x.Parameters}).ToList
            End If
        End With
        Return j
    End Function

    Public Shared Function ToSecurityUser(data As SecurityUser) As DataContract.SecurityUser
        Dim su As New DataContract.SecurityUser
        With su
            .SecurityUserId = data.SecurityUserId
            .Name = data.Name
            .Email = data.Email
            .IsGroup = data.IsGroup
            .Roles = data.Roles.Select(Function(x) New DataContract.SecurityRole With {.SecurityRoleId = x.SecurityRoleId,
                                                                                        .Name = x.Name,
                                                                                        .Description = x.Description}).ToList
        End With
        Return su
    End Function

    Public Shared Function FromSecurityUser(data As DataContract.SecurityUser) As SecurityUser
        Dim su As New SecurityUser
        With su
            .SecurityUserId = data.SecurityUserId
            .Name = data.Name
            .Email = data.Email
            .IsGroup = data.IsGroup
        End With
        Return su
    End Function

    Public Shared Function ToSecurityRole(data As SecurityRole) As DataContract.SecurityRole
        Dim sr As New DataContract.SecurityRole
        With sr
            .SecurityRoleId = data.SecurityRoleId
            .Name = data.Name
            .Description = data.Description

        End With
        Return sr
    End Function

    Public Function FromCronTask(data As iCronTask) As DataContract.CronTask
        Return New DataContract.CronTask With {.Guid = data.CronGuid, .Name = data.CronName, .Status = data.CronStatus.ToString}
    End Function

End Class
